<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ page import="Database.Document" %>
<%@ page import="Database.DocumentVector" %>
<%@ page import="InformationRetriever.Formulas" %>

<%@ include file="/header" %>

<%
    DocumentVector list = null;
    Document doc1 = null, doc2 = null;
    Formulas formula = null;
    String result = null;
    double value = 0.0d;
    int i = 0;
    
    try
    {
        list = (DocumentVector)request.getAttribute("list");
        value = ((Double)request.getAttribute("value")).doubleValue();
        doc1 = (Document)request.getAttribute("doc1");
        doc2 = (Document)request.getAttribute("doc2");
        result = (String)request.getAttribute("result");
    }
    catch(Exception e)
    {
        ///if the file list is null, redirect to the proper  page...
        response.sendRedirect(siteRoot+"/Compare");
    }
    
    if(     list == null
            || doc1 == null
            || doc2 == null
            || result == null
            || request.getAttribute("value") == null
            || request.getParameter("formula") == null
            || request.getParameter("doc1") == null
            || request.getParameter("doc2") == null)
    {
        response.sendRedirect(siteRoot+"/Compare");
    }
%>

    <div id="compareForm">
        Select two documents to perform a compare.
        <form action="<%=siteRoot%>/Compare" method="get">
            <p>
                <select name="doc1" alt="Documento 1" title="Document 1" size="15">
                    <%
                    i = 0;
                    for(Document doc : list)
                    {
                    %>
                    <option value="<%=i++%>" <% if(doc1.equals(doc)) out.write("selected"); %> /><%=i%>. <%=doc.toString()%>
                    <%
                    }
                    %>
                </select>
                <select name="doc2" alt="Documento 2" title="Document 2" size="15">
                    <%
                    i = 0;
                    for(Document doc : list)
                    {
                    %>
                    <option value="<%=i++%>" <% if(doc2.equals(doc)) out.write("selected"); %> /><%=i%>. <%=doc.toString()%>
                    <%
                    }
                    %>
                </select>
            </p>
            <p>
                <select name="formula" alt="Select a formula" title="Select a formula">
                    <option value="<%=Formulas.InnerProduct%>"      <% if(request.getParameter("formula").equals(Formulas.InnerProduct.toString())) {out.write("selected"); formula = Formulas.InnerProduct;} %> />      <%=Formulas.InnerProduct.title()%>
                    <option value="<%=Formulas.Cosine%>"            <% if(request.getParameter("formula").equals(Formulas.Cosine.toString())) {out.write("selected"); formula = Formulas.Cosine;} %> />            <%=Formulas.Cosine.title()%>
                    <option value="<%=Formulas.DiceCoefficient%>"   <% if(request.getParameter("formula").equals(Formulas.DiceCoefficient.toString())) {out.write("selected"); formula = Formulas.DiceCoefficient;} %> />   <%=Formulas.DiceCoefficient.title()%>
                    <option value="<%=Formulas.JaccardCoefficient%>"<% if(request.getParameter("formula").equals(Formulas.JaccardCoefficient.toString())) {out.write("selected"); formula = Formulas.JaccardCoefficient;} %> /><%=Formulas.JaccardCoefficient.title()%>
                    <option value="<%=Formulas.EuclideanDistance%>" <% if(request.getParameter("formula").equals(Formulas.EuclideanDistance.toString())) {out.write("selected"); formula = Formulas.EuclideanDistance;} %> /> <%=Formulas.EuclideanDistance.title()%>
                    <option value="<%=Formulas.ManhattanDistance%>" <% if(request.getParameter("formula").equals(Formulas.ManhattanDistance.toString())) {out.write("selected"); formula = Formulas.ManhattanDistance;} %> /> <%=Formulas.ManhattanDistance.title()%>
                </select>
                <input type="image" id="compareSubmitIcon" src="<%=siteRoot%>/compare/compare.png" alt="Compare documents" title="Compare documents" />
            </p>
        </form>
        <p>Or <a href="<%=siteRoot%>/Query">perform a query over the indexed document base</a>.</p>
    </div>
        
    <h2>Result</h2>
    
    <div id="resultCompare">
        The result of comparing documents "<%=doc1%>"
        and "<%=doc2%>" using the formula "<%=formula.title()%>"
        is that they are: <b><%=result%></b> (with a formula value of <b><%=value%></b>).
    </div>
    
<%@ include file="/footer" %>
